const removeNthFromEnd = function(head, n) {

  // 初始化dummy节点
  const dummy = new ListNode()
  dummy.next = head

  let slow = dummy
  let fast = dummy

  while (n !== 0) {
    fast = fast.next
    n--;
  }

  while (fast !== null) {
    slow = slow.next
    fast = fast.next
  }

  slow.next = slow.next.next

  return dummy.next
}

var removeNthFromEnd2 = function(head, n) {
  const findKthNode = function(head,n) {
    let p1 = head;
    for(let i = 0;i < n;i++) {
      p1 = p1.next;
    }
    let p2 = head;
    while(p1 != null) {
      p1 = p1.next;
      p2 = p2.next;
    }
    return p2;
  }
  let dummy = new ListNode(-1)
  dummy.next = head;
  let willDelBefore = findKthNode(dummy,n+1);
  willDelBefore.next = willDelBefore.next.next;
  return dummy.next
};